package com.initial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.entity.TbCrmEmailRecord;
import com.initial.entity.TbCrmTask;
import com.initial.entity.TbSystemUser;
import com.initial.service.TbCrmEmailRecordService;
import com.initial.service.TbCrmTaskService;
import com.initial.service.TbSystemUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    private TbCrmEmailRecordService tbCrmEmailRecordService;
    @Resource
    private TbCrmTaskService tbCrmTaskService;
    /**
     * 注销 退出
     * */
    @RequestMapping("system/user/UserAction_logout.action")
    public String UserAction_logout(HttpSession session){
        session.invalidate();
        return "redirect:/toLogin";
    }

    /**
     *  首页  去查看   分页查询  待执行的任务  任务表
     * */
    @RequestMapping("manage/task/index_ToFindByPage")
    public String tolist_task(){
        return "views/system/index_ditty/list_task_index";
    }
    /**
     * 任务表  分页查询  待执行的任务
     * */
    @RequestMapping("manage/task/index_TaskAction_findByPage.action")
    @ResponseBody
    public Map TaskAction_findByPage(Integer page, Integer rows, String asd, String subject, HttpSession session){
        Map map=new HashMap();
        if (asd!=null && asd.equals("1")){
            asd="subject";
        }
        TbSystemUser user = (TbSystemUser) session.getAttribute("user");
        List<TbCrmTask> tbCrmTasks = tbCrmTaskService.indexselectAllPage(page,rows,asd,subject,user.getId(),"待执行");
        int size = tbCrmTasks.size();
        map.put("totalRows",size);
        map.put("result",tbCrmTasks);
        return map;
    }
    /**
     *  首页  去查看   分页查询  待执行的任务  任务表
     * */
    @RequestMapping("manage/task/index_tow_ToFindByPage")
    public String tolist_task_tow(){
        return "views/system/index_ditty/list_task_index_tow";
    }
    /**
     * 任务表  分页查询  待执行的任务
     * */
    @RequestMapping("manage/task/index_tow_TaskAction_findByPage.action")
    @ResponseBody
    public Map TaskAction_findByPage_tow(Integer page, Integer rows, String asd, String subject, HttpSession session){
        Map map=new HashMap();
        if (asd!=null && asd.equals("1")){
            asd="subject";
        }
        TbSystemUser user = (TbSystemUser) session.getAttribute("user");
        List<TbCrmTask> tbCrmTasks = tbCrmTaskService.indexselectAllPage(page,rows,asd,subject,user.getId(),null);
        int size = tbCrmTasks.size();
        map.put("totalRows",size);
        map.put("result",tbCrmTasks);
        return map;
    }
    /**
     * 去  ***   查看发送邮箱的记录 *** 分页查询
     * */
    @RequestMapping("manage/marketing/index_MarketingAction_sendemail.do")
    public String MarketingActionSendemail(){
        return "views/system/index_ditty/index_list_message";
    }
    /**
     * 分页查询 *****    发邮箱的记录
     * */
    @RequestMapping("system/message/index_MessageAction_findByPage.action")
    @ResponseBody
    public Map MessageActionFindByPageAction(Integer page,Integer rows,HttpSession session){
        Map map=new HashMap();
        TbSystemUser user = (TbSystemUser) session.getAttribute("user");
        Integer userId = user.getId();
        IPage<TbCrmEmailRecord> tbCrmEmailRecordIPage = tbCrmEmailRecordService.selectAllPage(new Page<TbCrmEmailRecord>(page, rows),userId);
        long total = tbCrmEmailRecordIPage.getTotal();
        List<TbCrmEmailRecord> records = tbCrmEmailRecordIPage.getRecords();
        map.put("totalRows",total);
        map.put("result",records);
        return map;
    }
}
